package com.lu.stochastic31.activity;

import com.actionbarsherlock.app.SherlockFragmentActivity;
import com.actionbarsherlock.view.Menu;
import com.actionbarsherlock.view.MenuInflater;
import com.actionbarsherlock.view.MenuItem;
import com.lu.stochastic31.R;
import com.lu.stochastic31.content.SQLiteHelper;
import com.lu.stochastic31.fragment.RandomDetailFragment;
import com.lu.stochastic31.fragment.RandomListFragment;

import android.content.Intent;
import android.os.Bundle;
import android.widget.ListView;

/**
 * see <code>refs.xml</code> in values folders.
 * 
 * @author chobitly
 * 
 */
public class RandomListActivity extends SherlockFragmentActivity implements
		RandomListFragment.Callbacks {

	/** flag for two pane layout. */
	private boolean mTwoPane;
	/** always shown. */
	private RandomListFragment randomListFragment;
	/** shown in two pane layout. */
	private RandomDetailFragment randomDetailFragment;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);

		SQLiteHelper.getInstance(getApplicationContext()).refreshFileNames();
		// TODO What to do when there are to many files?

		setContentView(R.layout.activity_random_list);
		// setContentView(R.layout.activity_random_twopane);

		getSupportActionBar().setTitle(R.string.title_random_list);

		randomListFragment = (RandomListFragment) getSupportFragmentManager()
				.findFragmentById(R.id.random_list);
		if (randomListFragment == null)
			throw new IllegalStateException(
					"Activity must contain an instance of RandomListFragment.");

		if (findViewById(R.id.random_detail_container) != null) {
			mTwoPane = true;
			randomListFragment.setActivateOnItemClick(true);

			// Set ActivatedPosition when this Activity load for the first time.
			if (randomListFragment.getActivatedPosition() == ListView.INVALID_POSITION
					&& randomListFragment.getListView().getCount() > 0)
				randomListFragment.setActivatedPosition(0);

			randomListFragment.getListView().performItemClick(
					randomListFragment.getListView(),
					randomListFragment.getActivatedPosition(),
					randomListFragment.getListView().getItemIdAtPosition(
							randomListFragment.getActivatedPosition()));
		}
	}

	@Override
	protected void onDestroy() {
		{// Close Databases
			SQLiteHelper sqLiteHelper = SQLiteHelper.getInstance(this);
			try {
				sqLiteHelper.getReadableDatabase().close();
			} catch (Exception e) {
			}
			try {
				sqLiteHelper.getWritableDatabase().close();
			} catch (Exception e) {
			}
		}

		super.onDestroy();
	}

	@Override
	public void onItemSelected(long name_index) {
		if (mTwoPane) {// When mTwoPane==true, replace RandomDetailFragment
			Bundle arguments = new Bundle();
			arguments.putLong(RandomDetailFragment.ARG_ITEM_NAME_INDEX,
					name_index);
			randomDetailFragment = new RandomDetailFragment();
			randomDetailFragment.setArguments(arguments);
			getSupportFragmentManager()
					.beginTransaction()
					.replace(R.id.random_detail_container, randomDetailFragment)
					.commit();
		} else {// When mTwoPane==false, start RandomDetailActivity
			Intent detailIntent = new Intent(this, RandomDetailActivity.class);
			detailIntent.putExtra(RandomDetailFragment.ARG_ITEM_NAME_INDEX,
					name_index);
			startActivity(detailIntent);
		}
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		switch (item.getItemId()) {
		case R.id.menu_run_random:
			if (mTwoPane && randomDetailFragment != null)
				return randomDetailFragment.onOptionsItemSelected(item);
			break;
		default:
			if (randomListFragment != null)
				return randomListFragment.onOptionsItemSelected(item);
			break;
		}
		return super.onOptionsItemSelected(item);
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		MenuInflater inflater = getSupportMenuInflater();
		// TODO Let the Fragments use add() to add MenuItems.
		if (mTwoPane) {
			randomDetailFragment.onCreateOptionsMenu(menu, inflater);
		} else {
			randomListFragment.onCreateOptionsMenu(menu, inflater);
		}
		return true;
	}
}
